package com.iwhalecloud.bss.kite.cucc.service.shoppingcart.query;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.cart.dataservice.entity.ShoppingCartItem;
import com.iwhalecloud.bss.kite.client.api.shoppingcart.operate.IOperateShoppingCartItemService;
import com.iwhalecloud.bss.kite.client.api.shoppingcart.query.IQueryShoppingCartItemService;
import com.iwhalecloud.bss.kite.client.dto.shoppingcart.ShoppingCartItemDTO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.KiteShoppingCartItem;
import com.iwhalecloud.bss.kite.dataservice.mapper.shoppingcart.ShoppingCartItemMapper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.dto.KiteSceneInstDTO;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import tk.mybatis.mapper.entity.Example;

import com.ztesoft.bss.common.util.SpringUtil;

@Service
public class QueryShoppingCartItemService implements IQueryShoppingCartItemService {

    @Autowired
    ShoppingCartItemMapper shoppingCartItemMapper;

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public List<ShoppingCartItemDTO> listAllPayingShoppingCartItemDTOs(KiteSceneInstDTO kiteSceneInstDTO, String groupId, String custId) {
        if (Objects.isNull(kiteSceneInstDTO)) {
            kiteSceneInstDTO = querySceneInstService.getSceneInstDTO(groupId);
        }
        Example example = new Example(KiteShoppingCartItem.class);
        example.createCriteria().andEqualTo("groupId", groupId).andEqualTo("custId", custId);
        List<KiteShoppingCartItem> shoppingCartItems = this.shoppingCartItemMapper.selectByExample(example);
        return transferToShoppingCartItemDTOs(kiteSceneInstDTO, shoppingCartItems);
    }

    private List<ShoppingCartItemDTO> transferToShoppingCartItemDTOs(KiteSceneInstDTO kiteSceneInstDTO, List<KiteShoppingCartItem> shoppingCartItems) {
        if (KiteListUtils.isEmpty(shoppingCartItems)) {
            return null;
        }
        List<ShoppingCartItemDTO> shoppingCartItemDTOs = new ArrayList<ShoppingCartItemDTO>();
        for (KiteShoppingCartItem item : shoppingCartItems) {
            KiteInst kiteInst = getTargetKiteInst(kiteSceneInstDTO, item.getCartItemCd(), item.getObjId());
            if (Objects.nonNull(kiteInst)) {
                shoppingCartItemDTOs.add(createShoppingCartItemDTO(item, kiteInst));
            }
        }
        return shoppingCartItemDTOs;
    }

    private ShoppingCartItemDTO createShoppingCartItemDTO(KiteShoppingCartItem item, KiteInst inst) {
        ShoppingCartItemDTO itemDTO = new ShoppingCartItemDTO();
        KiteBeanUtils.copyProperties(item, itemDTO);

        IOperateShoppingCartItemService operateShoppingCartItemService = SpringUtil.getBean(IOperateShoppingCartItemService.class);
        operateShoppingCartItemService.setItemInfoFromKiteInst(itemDTO, inst);
        return itemDTO;
    }

    private KiteInst getTargetKiteInst(KiteSceneInstDTO kiteSceneInstDTO, String cartItemCd, String objId) {
        List<KiteInst> kiteInsts = new ArrayList<KiteInst>();
        if (KeyConsts.SHOPPING_CART_ITEM_CD_XSP.equals(cartItemCd)) {
            kiteInsts.addAll(kiteSceneInstDTO.getAdditionalOfferInsts());
            kiteInsts.addAll(kiteSceneInstDTO.getPromotionOfferInsts());
            kiteInsts.addAll(kiteSceneInstDTO.getMainOfferInsts());
            kiteInsts.addAll(kiteSceneInstDTO.getGiftOfferInsts());
        }
        else if (KeyConsts.SHOPPING_CART_ITEM_CD_CP.equals(cartItemCd)) {
            kiteInsts.addAll(kiteSceneInstDTO.getProdInsts());
        }
        else if (KeyConsts.SHOPPING_CART_ITEM_CD_GNCP.equals(cartItemCd)) {
            kiteInsts.addAll(kiteSceneInstDTO.getProdInstFuncs());
        }
        else if (KeyConsts.SHOPPING_CART_ITEM_CD_YXZY.equals(cartItemCd)) {
            kiteInsts.addAll(kiteSceneInstDTO.getMktResInsts());
        }
        return getKiteInst(kiteInsts, objId);
    }

    private KiteInst getKiteInst(List<KiteInst> kiteInsts, String objId) {
        if (KiteListUtils.isEmpty(kiteInsts)) {
            return null;
        }
        for (KiteInst kiteInst : kiteInsts) {
            if (StringUtils.equals(kiteInst.getInstId(), objId)) {
                return kiteInst;
            }
        }
        return null;
    }

    @Override
    public List<ShoppingCartItemDTO> listShoppingCartItemDtos(String groupId, String custId) {
        Example example = new Example(KiteShoppingCartItem.class);
        example.createCriteria().andEqualTo("groupId", groupId).andEqualTo("custId", custId);
        List<KiteShoppingCartItem> shoppingCartItems = this.shoppingCartItemMapper.selectByExample(example);
        if (KiteListUtils.isEmpty(shoppingCartItems)) {
            shoppingCartItems = this.shoppingCartItemMapper.selectShoppingCartItemsFromHis(groupId, custId);
        }
        return KiteBeanUtils.copyPropsForList(shoppingCartItems, ShoppingCartItemDTO.class);
    }
}
